package com.atguigu.guli.service.statistics.service.impl;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.statistics.entity.Daily;
import com.atguigu.guli.service.statistics.feign.UcenterMemberService;
import com.atguigu.guli.service.statistics.mapper.DailyMapper;
import com.atguigu.guli.service.statistics.service.DailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author Helen
 * @since 2020-07-11
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    @Override
    public void createStatisticsByDay(String day) {

        //方案一：查询数据如果存在则删除
        QueryWrapper<Daily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated", day);
        baseMapper.delete(queryWrapper);

        //方案二：查询数据如果存在则退出

        //调用远程方法统计相关数据
        R r = ucenterMemberService.countRegisterNum(day);
        Integer registerNumber = (Integer)r.getData().get("registerNum");

        //将统计数据插入到统计表中
        Daily daily = new Daily();
        daily.setRegisterNum(registerNumber);
        daily.setCourseNum(RandomUtils.nextInt(100, 300));
        daily.setVideoViewNum(RandomUtils.nextInt(100, 300));
        daily.setLoginNum(RandomUtils.nextInt(100, 300));
        daily.setDateCalculated(day);

        baseMapper.insert(daily);
    }

    @Override
    public Map<String, Map<String, Object>> getChartData(String begin, String end) {

        Map<String, Map<String, Object>> map = new HashMap<>();
        Map<String, Object> registerNum =  this.getChartDataByType(begin, end, "register_num");
        Map<String, Object> loginNum =  this.getChartDataByType(begin, end, "login_num");
        Map<String, Object> videoViewNum =  this.getChartDataByType(begin, end, "video_view_num");
        Map<String, Object> courseNum =  this.getChartDataByType(begin, end, "course_num");
        map.put("registerNum", registerNum);
        map.put("loginNum", loginNum);
        map.put("videoViewNum", videoViewNum);
        map.put("courseNum", courseNum);

        return map;
    }

    private Map<String, Object> getChartDataByType(String begin, String end, String type){
        Map<String, Object> map = new HashMap<>();

        List<String> xList = new ArrayList<>();
        List<Integer> yList = new ArrayList<>();

        QueryWrapper<Daily> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(type, "date_calculated");
        queryWrapper.between("date_calculated", begin, end);

        List<Map<String, Object>> mapsData = baseMapper.selectMaps(queryWrapper);
        for (Map<String, Object> data : mapsData) {
            String dateCalculated = (String)data.get("date_calculated");
            xList.add(dateCalculated);

            Integer count = (Integer)data.get(type);
            yList.add(count);
        }

        map.put("xList", xList);
        map.put("yList", yList);

        return map;
    }
}
